﻿using System;
using System.Collections.Generic;
using System.Windows.Media;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Collections.ObjectModel;
using System.Windows.Data;

namespace QuanLyDuocPham.Controllers.QuanLyCauHinh.NguoiDung
{

    public class USER_Control : IDisposable
    {
        DuocPhamDataContext db = new DuocPhamDataContext(staticClass.Conn);
        
        public void Dispose()
        {
            if (db != null)
            {
                db.Dispose();
                db = null;
            }
            if (nhomndctrl != null)
            {
                nhomndctrl.Dispose();
                nhomndctrl = null;
            }
        }
        public int DangNhap(string username, string password)
        {
            int result = 0;
            var ls = db.NHANVIEN_GetByTenDN(username);

            if (ls.Count() == 0)
                result = 0;//Khong ton tai nguoi dung nay

            else
            {
                var us = (NHANVIEN_GetByTenDNResult)db.NHANVIEN_GetByTenDN(username).Single();
                if (us.TRANG_THAI == false)
                    result = 3; //Nguoi dung bi khoa
                String matkhau_hethong = us.MAT_KHAU;//MNat khau trong CSDL
                try
                {
                    if (matkhau_hethong != password)
                    {
                        result = 1;//Sai mat khau
                    }
                    else
                    {
                        NHANVIEN_GetByMaNhomndResult ndinfo = new NHANVIEN_GetByMaNhomndResult();
                        ndinfo.HO_TEN = us.HO_TEN;
                        ndinfo.TEN_DN = us.TEN_DN;
                        ndinfo.MAT_KHAU = us.MAT_KHAU;
                        ndinfo.MA_NHOMND = us.MA_NHOMND;
                        ndinfo.TRANG_THAI = us.TRANG_THAI;

                        staticClass.nguoidungStatic = ndinfo;
                        result = 2;//Dang nhap thanh cong
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Lỗi:" + ex.Message);
                }
            }
            return result;
        }

        public void addData(ComboBox cmbHo_Ten, TextBox txtTen_DN, PasswordBox txtMat_Khau, ComboBox cmbNhomND, CheckBox chbTrang_Thai)
        {
            int result = validateData( txtTen_DN, txtMat_Khau);
            if (result == 1)
            {

                var ten_nv = (NHANVIEN_Get_ListResult)cmbHo_Ten.SelectedItem;
                var nhom_nd = (NHOMND_Get_ListResult)cmbNhomND.SelectedItem;
                db.USER_Add(
                    ten_nv.MA_NHANVIEN,
                    nhom_nd.MA_NHOMND,
                    txtTen_DN.Text,
                    txtMat_Khau.Password,
                    chbTrang_Thai.IsChecked
                    );

                MessageBox.Show("Đã thêm thành công!");
                cmbHo_Ten.SelectedItem = null;
                txtTen_DN.Clear();
                txtMat_Khau.Clear();
                cmbNhomND.SelectedItem = null;
                chbTrang_Thai.IsChecked = false;
            }
            else if (result == 0)
            {
                MessageBox.Show("Dữ liệu không hợp lệ!");
            }
            else if (result == 3)
                MessageBox.Show("Mật khẩu phải gồm ít nhất 6 kí tự");
            else MessageBox.Show("Tên đăng nhập này đã được đăng kí! Bạn hãy chọn tên đăng nhập khác");
        }

        public void updateData(string ma_nv, TextBox txtTen_DN, PasswordBox txtMat_Khau, ComboBox cmbNhomND, CheckBox chbTrang_Thai)
        {
            int result = validateData(txtTen_DN, txtMat_Khau);

            if (txtTen_DN.Text.Trim() != "" && txtMat_Khau.Password.Trim() != "" &&
                cmbNhomND.SelectedItem != null )
            {
                
                var nhom_nd = (NHOMND_Get_ListResult)cmbNhomND.SelectedItem;
                db.USER_Add(
                    ma_nv,
                    nhom_nd.MA_NHOMND,
                    txtTen_DN.Text,
                    txtMat_Khau.Password,
                    chbTrang_Thai.IsChecked
                    );

                MessageBox.Show("Đã cập nhật thành công!");
            }
            else
            {
                MessageBox.Show("Dữ liệu không hợp lệ!");
            }
        }

        public void deleteData(string ma_nv)
        {            
                
                db.USER_Add(
                    ma_nv,
                    null,
                    null,
                    null,
                    null
                    );

                MessageBox.Show("Đã xóa người dùng thành công!");
            
        }

        private int validateData( TextBox txtTen_DN, PasswordBox txtMat_Khau)
        {
            if (txtTen_DN.Text.Trim() == "" || txtMat_Khau.Password.Trim() == "" )
            {
                return 0;
            }
            else
            {
                if (txtMat_Khau.Password.Length < 6)
                    return 3;
                foreach (var t in db.NHANVIEN_Get_List())
                {
                    if (txtTen_DN.Text == t.TEN_DN)
                    {
                        return 2;
                    }
                }
            }
            
            return 1;
        }

        public void loadNGUOIDUNG(ListView lvNGUOIDUNG_List, DataPager dp)
        {

            var types = db.USER_Get_List();
            ObservableCollection<Object> dpSource = new ObservableCollection<Object>();

            var typeorder = types.OrderBy(w => w.HO_TEN);
            int i = 1;
            foreach (var u in typeorder)
            {
                if (u.TEN_DN != null)
                {
                    dpSource.Add(new { STT = i, u.HO_TEN, u.TEN_DN, u.MAT_KHAU, u.TRANG_THAI, u.DIEN_GIAI });
                    i++;
                }
            }
            dp.ItemsSource = dpSource;
            dp.ItemsPerPage = 10;
            
        }

        
        public void load_NGUOIDUNG(ListView lvNGUOIDUNG_List)
        {
           
            var types = db.USER_Get_List();
            lvNGUOIDUNG_List.Items.Clear();
            var typeorder = types.OrderBy(w => w.HO_TEN);
            
            
            int i = 1;
            foreach (var u in typeorder)
            {
                if (u.TEN_DN != null)
                {
                    ListViewItem item = new ListViewItem();
                    var stt = new { STT = i, u.HO_TEN, u.TEN_DN, u.MAT_KHAU, u.TRANG_THAI, u.DIEN_GIAI };
                    item.Content = stt;
                    ;
                    item.ToolTip = createTooltip(u);
                    lvNGUOIDUNG_List.Items.Add(item);
                    i++;
                }
                
            }
            
            
        }

        void view_Filter(object sender, FilterEventArgs e)
        {
            //int index = customers.IndexOf((Customer)e.Item);

            //if (index >= itemPerPage * currentPageIndex && index < itemPerPage * (currentPageIndex + 1))
            //{
            //    e.Accepted = true;
            //}
            //else
            //{
            //    e.Accepted = false;
            //}
        }
        private Grid createTooltip(USER_Get_ListResult u)
        {
            Grid gr = new Grid();
                        

            //Ten Nhan Vien
            TextBlock txtho_ten = new TextBlock();
            txtho_ten.FontSize = 16;
            txtho_ten.Width = 120;
            txtho_ten.FontWeight = FontWeights.Bold;
            txtho_ten.Foreground = new BrushConverter().ConvertFromString("#FF116A98") as SolidColorBrush;
            txtho_ten.Height = 20;
            txtho_ten.Text = u.HO_TEN;
            txtho_ten.Margin = new Thickness(100, 22, 10, 0);
            txtho_ten.TextAlignment = TextAlignment.Center;
            txtho_ten.TextWrapping = TextWrapping.Wrap;
            txtho_ten.VerticalAlignment = VerticalAlignment.Top;

            //Ten Dang Nhap              
            TextBlock txtTen_DN = new TextBlock();
            txtTen_DN.Height = 16;
            txtTen_DN.Width = 100;
            txtTen_DN.HorizontalAlignment = HorizontalAlignment.Right;
            txtTen_DN.FontSize = 12;
            txtTen_DN.FontWeight = FontWeights.Bold;
            txtTen_DN.Text = u.TEN_DN;
            txtTen_DN.Margin = new Thickness(0, 49, 10, 0);
            txtTen_DN.TextAlignment = TextAlignment.Left;
            txtTen_DN.VerticalAlignment = VerticalAlignment.Top;

            //Mat Khau            
            TextBlock txtMat_Khau = new TextBlock();
            txtMat_Khau.Height = 16;
            txtMat_Khau.Width = 100;
            txtMat_Khau.FontSize = 12;
            txtMat_Khau.FontWeight = FontWeights.Bold;
            txtMat_Khau.Margin = new Thickness(100, 49, 100, 0);
            txtMat_Khau.TextAlignment = TextAlignment.Left;
            txtMat_Khau.VerticalAlignment = VerticalAlignment.Top;
            txtMat_Khau.Text = u.MAT_KHAU;

            //Nhom nguoi dung
            TextBlock txtNhom_ND = new TextBlock();
            txtNhom_ND.Height = 16;
            txtNhom_ND.Width = 120;
            txtNhom_ND.FontSize = 12;
            txtNhom_ND.FontWeight = FontWeights.Bold;
            txtNhom_ND.FlowDirection = FlowDirection.RightToLeft;
            txtNhom_ND.Margin = new Thickness(0, 0, 0, 0);
            txtNhom_ND.TextAlignment = TextAlignment.Left;
            txtNhom_ND.VerticalAlignment = VerticalAlignment.Top;
            txtNhom_ND.HorizontalAlignment = HorizontalAlignment.Right;
            txtNhom_ND.Text = u.DIEN_GIAI;

            //Trang Thai
            TextBlock txtTrang_Thai = new TextBlock();
            txtTrang_Thai.Height = 16;
            txtTrang_Thai.FontSize = 12;
            txtTrang_Thai.Width = 100;
            txtTrang_Thai.FontWeight = FontWeights.Bold;
            txtTrang_Thai.Margin = new Thickness(160, 76, 12, 0);
            txtTrang_Thai.VerticalAlignment = VerticalAlignment.Top;
            if (u.TRANG_THAI == true)
            {
                txtTrang_Thai.Text = "Hoạt động";
            }
            else
                txtTrang_Thai.Text = "Bị Khóa";

            gr.Children.Add(txtho_ten);
            gr.Children.Add(txtTen_DN);
            gr.Children.Add(txtMat_Khau);
            gr.Children.Add(txtNhom_ND);
            gr.Children.Add(txtTrang_Thai);
            

            return gr;
        }
        NHOMND_Control nhomndctrl = new NHOMND_Control();
        public void loadUSER_Edit(string ma_nv, TextBox txtHo_Ten, TextBox txtTen_DN, PasswordBox txtMat_Khau, ComboBox cmbNhomND, CheckBox chbTrang_Thai)
        {

            var us = (USER_GetByMaNVResult)db.USER_GetByMaNV(ma_nv).Single();


            txtHo_Ten.Text = us.HO_TEN;
            txtTen_DN.Text = us.TEN_DN;
            txtMat_Khau.Password = us.MAT_KHAU;
            nhomndctrl.loadComboBox(cmbNhomND);
            int t = 0;
            foreach (var i in cmbNhomND.Items)
            {
                var temp = (NHOMND_Get_ListResult)i;
                if (temp.MA_NHOMND == us.MA_NHOMND)
                {
                    break;
                }
                t++;
            }
            cmbNhomND.SelectedIndex = t;
            chbTrang_Thai.IsChecked = us.TRANG_THAI;
        }

    }
}
